﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;

namespace WAD.DAL.Union
{
   public class DAO_Union
    {
        #region 其他数据库操作，非连接池
        static string ConnectionString_SHT = ConfigurationManager.ConnectionStrings["DBConnectionString_SHT"].ConnectionString;

        public static Dictionary<string, object> Get(string tableName, string where, string fileds = "")
        {
            if (string.IsNullOrEmpty(fileds))
                fileds = "*";
            if (!string.IsNullOrEmpty(where))
                where = " where " + where;
            IDataReader dr = null;
            Dictionary<string, object> map = null;
            try
            {
                dr = QD.DataBase.DBHelper.MSSQLHelper.Instance.ExecuteReader(ConnectionString_SHT, CommandType.Text, string.Format("select top 1 {0} from dbo.[{1}] {2} ", fileds, tableName, where));
                while (dr.Read())
                {
                    map = new Dictionary<string, object>();
                    //遍历列
                    for (int i = 0; i < dr.FieldCount; i++)
                    {
                        map.Add(dr.GetName(i), dr[i]);
                    }
                }
            }
            catch { }
            finally { if (dr != null)dr.Close(); }
            return map;
        }

        public static List<Dictionary<string, object>> GetList(String sql)
        {
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            using (IDataReader dr = QD.DataBase.DBHelper.MSSQLHelper.Instance.ExecuteReader(ConnectionString_SHT, CommandType.Text, sql, null))
            {
                while (dr.Read())
                {
                    Dictionary<string, object> map = new Dictionary<string, object>();
                    for (int i = 0; i < dr.FieldCount; i++)   //遍历列
                    {
                        map.Add(dr.GetName(i), dr[i]);
                    }
                    list.Add(map);
                }
            }
            return list;
        }

        public static DataTable ExecuteDataTable(string sql, IDbDataParameter[] param = null)
        {
            return QD.DataBase.DBHelper.MSSQLHelper.Instance.ExecuteDataTable(ConnectionString_SHT, CommandType.Text, sql, param);
        }

        public static object ExecuteScalar(string sql, IDbDataParameter[] param = null)
        {
            return QD.DataBase.DBHelper.MSSQLHelper.Instance.ExecuteScalar(ConnectionString_SHT, CommandType.Text, sql, param);
        }

        public static int ExecuteNonQuery(string sql, IDbDataParameter[] param = null)
        {
            return QD.DataBase.DBHelper.MSSQLHelper.Instance.ExecuteNonQuery(ConnectionString_SHT, CommandType.Text, sql, param);
        }
        public static int ExecuteNonQuery_Stored(string StoredProcedure, IDbDataParameter[] param = null)
        {
            return QD.DataBase.DBHelper.MSSQLHelper.Instance.ExecuteNonQuery(ConnectionString_SHT, CommandType.StoredProcedure, StoredProcedure, param);
        }
        #endregion
    }
}
